'use strict';
var config = require('../config/config');

function request(url, method, data, cb) {
  var header = {
    'Authorization': 'Bearer ' + config.token,
    'appId':'sdfsdfsdfds',
    'Content-Type': 'application/json',  
  };
  var sendUrl=config.host_url+url;
  wx.request({
    url: sendUrl,
    data: data,
    method: method || 'GET',
    header: header,
    success: function success(res) {
      if (res && res.statusCode == 200) {
        if (res.data.result) {
          typeof cb == "function" && cb(res.data.data);
        } else {
          /*var err = { msg: res.data.errorMessage };
          typeof cb == "function" && cb(undefined, err);*/
        	var msg=res.data.errorMessage
        	wx.showToast({
                title: msg,
                icon: 'succes',
                duration: 2000,
                mask:true
            })
        }
      } else {
        var err = { msg: res.errMsg };
        typeof cb == "function" && cb(undefined, err);
      }
    },
    fail: function fail(error) {
    	 wx.showToast({
             title: '服务器调用失败',
             icon: 'succes',
             duration: 2000,
             mask:true
         })
      console.log("wx.request error:", error);
   /*   var err = { msg: error.errorMessage };
      typeof cb == "function" && cb(undefined, err);*/
    }
  });
}

module.exports = {
  // 请求方法
  request: request
};
